package com.xx.xiawan.videoplayer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.pili.pldroid.player.PLMediaPlayer;
import com.xx.xiawan.videoplayer.XXVideoPlayer;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private boolean needResume;
    private XXVideoPlayer mVideoView;
    private boolean mIsPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //  ButterKnife.inject(this);
        /*
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.video_activity_main);
        XXVideoView videoView = (XXVideoView)this.findViewById(R.id.viVideoPlayer);
        mVideoView = new XXVideoPlayer(this,videoView);
       // mVideoView.setOnInfoListener(mOnInfoListener);
        String mVideoPath = "/mnt/sdcard/mp4.mp4";
        mVideoView.setVideoPath(mVideoPath);
        ImageView img = (ImageView)this.findViewById(R.id.img_start);
        img.setOnClickListener(this);
        */
    }

    private PLMediaPlayer.OnInfoListener mOnInfoListener = new PLMediaPlayer.OnInfoListener() {
        @Override
        public boolean onInfo(PLMediaPlayer plMediaPlayer, int what, int extra) {
            switch (what) {
                case PLMediaPlayer.MEDIA_INFO_BUFFERING_START:
//                    Logger.i("正在缓冲----");
                    System.out.println("正在缓冲----\"");
                    //开始缓存，暂停播放
//                    if (isPlaying()) {
//                        stopPlayer();
                    if (mVideoView != null) {
                        mVideoView.pause();
                    }
                    needResume = true;
//                    }
                    //   mLoadingView.setVisibility(View.VISIBLE);
                    break;
                case PLMediaPlayer.MEDIA_INFO_BUFFERING_END:
                case PLMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
//                    Logger.i("缓冲完成----");
                    System.out.println("缓冲完成----");
                    //缓存完成，继续播放
                    if (needResume)
//                        startPlayer();
                        if (mVideoView != null) {
                            mVideoView.start();
                        }
                    //    mLoadingView.setVisibility(View.GONE);
                    break;
                case PLMediaPlayer.MEDIA_INFO_BUFFERING_BYTES_UPDATE:
                    //显示 下载速度
//                    Logger.e("download rate:" + extra);
                    System.out.println("download rate:" + extra);
                    //mListener.onDownloadRateChanged(arg2);
                    break;
                case 10001:
                    //保存视频角度
                    //  mVideoRotation=extra;

                    break;
            }
//            Logger.i("onInfo:" + what + "___" + extra);
            System.out.println("onInfo:" + what + "___" + extra);
            return false;
        }
    };
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(!mIsPlayer){
            mIsPlayer = true;
            mVideoView.start();
        }else{
            mIsPlayer = false;
            mVideoView.pause();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mVideoView != null)
            mVideoView.pause();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mVideoView != null){
              	mVideoView.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mVideoView != null)
            mVideoView.stopPlayback();
    }
}
